package com.base.po.ypt_sdk;

import com.alibaba.fastjson.JSON;
import com.base.helper.WebHelper;
import com.base.po.param.NetState;
import com.base.po.ypt.*;
import com.base.utils.GsonUtil;
import com.base.utils.PayCommonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/replacement")
public class YPTLandmerchant {
    /**
     *YPT落地商户可用省查询
     */
    @RequestMapping(value = "/getProvince", method = { RequestMethod.POST, RequestMethod.GET })
    public @ResponseBody NetState getProvince(HttpServletRequest request){
        NetState netState = new NetState();

        // 签名值
        SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();
        parameterMap.put("merchant_no" , YPTConstants.YPT_MCHID);// 渠道商商户号YPT分配的渠道商户号
        parameterMap.put("channel_no" , YPTConstants.NEWREPAY_CHANNEL_NO);
        parameterMap.put("business_no" , YPTConstants.NEWREPAY_BUSINESS_NO);
        String sign = PayCommonUtil.createSign("UTF-8", parameterMap, YPTConstants.YPT_KEY);
        // 参数拼接
        List<String> params = new ArrayList<>();
        params.add("merchant_no=" + YPTConstants.YPT_MCHID);
        params.add("channel_no=" + YPTConstants.NEWREPAY_CHANNEL_NO);
        params.add("business_no=" + YPTConstants.NEWREPAY_BUSINESS_NO);
        String param = YPTUtil.getParam(params, sign);
        // 接口访问
        String result = null;
        try {
            result = WebHelper.PostData(YPTConstants.LANDMERCHANT_GETPROVINCE, param, null, null);
        } catch (Exception e) {
            netState.setErrorCode(NetState.FEAILED);
            netState.setData("可用省查询异常");
        }

        GetProvinceResponse response= JSON.parseObject(result, GetProvinceResponse.class);
        if (response.getResp_code() != null && response.getResp_code().equals("40000")){
            List<GetProvinceResponseData> data = response.getData();
            netState.setErrorCode(NetState.SUCCESS_CONN);
            netState.setData("可用省查询成功");
            netState.setJson(data);
        }else{
            netState.setErrorCode(NetState.FEAILED);
            netState.setData("可用省查询失败");
        }


        return netState;
    }

    /**
     *YPT落地商户可用城市查询
     */
    @RequestMapping(value = "/getCity", method = { RequestMethod.POST, RequestMethod.GET })
    public @ResponseBody NetState getCity(String provinceCode){
        NetState netState = new NetState();

        // 签名值
        SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();
        parameterMap.put("merchant_no" , YPTConstants.YPT_MCHID);// 渠道商商户号YPT分配的渠道商户号
        parameterMap.put("channel_no" , YPTConstants.NEWREPAY_CHANNEL_NO);
        parameterMap.put("business_no" , YPTConstants.NEWREPAY_BUSINESS_NO);
        parameterMap.put("province_code" , provinceCode);
        String sign = PayCommonUtil.createSign("UTF-8", parameterMap, YPTConstants.YPT_KEY);
        // 参数拼接
        List<String> params = new ArrayList<>();
        params.add("merchant_no=" + YPTConstants.YPT_MCHID);
        params.add("channel_no=" + YPTConstants.NEWREPAY_CHANNEL_NO);
        params.add("business_no=" + YPTConstants.NEWREPAY_BUSINESS_NO);
        params.add("province_code=" + provinceCode);
        String param = YPTUtil.getParam(params, sign);
        // 接口访问
        String result = null;
        try {
            result = WebHelper.PostData(YPTConstants.LANDMERCHANT_GETCITY, param, null, null);
        } catch (Exception e) {
            netState.setErrorCode(NetState.FEAILED);
            netState.setData("可用省查询异常");
        }

        GetCityResponse response= JSON.parseObject(result, GetCityResponse.class);
        System.out.println(result);
        if (response.getResp_code() != null && response.getResp_code().equals("40000")){
            List<GetCityResponseData> data = response.getData();
            netState.setErrorCode(NetState.SUCCESS_CONN);
            netState.setData("可用城市查询成功");
            netState.setJson(data);
        }else{
            netState.setErrorCode(NetState.FEAILED);
            netState.setData("可用城市查询失败");
        }

        return netState;
    }
}
